SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[buscarparecidos] 
 
-- Version 0.1 beta
-- Creado por Marcelo D. Otero el 03/10/07 
-- Dado un documento busca las posibles combinaciones con documentos que difieren en un digito
 
@ENT INT --documento
 
 AS
SET NOCOUNT ON
SET DATEFORMAT DMY
DECLARE @len INT
DECLARE @i INT
DECLARE @docu INT
DECLARE @die int
 
SET @len=len(cast(@ent AS VARCHAR))
SET @i=1
 
CREATE TABLE #TEMPteta(X INT)
while @i<=@len
   BEGIN
  SET @die=0
   WHILE @die<=9
       begin
                         INSERT INTO #TEMPteta VALUES(stuff(@ent,@i,1,@die))                
             SET @die=@die+1 
                   END
                SET @I=@I+1
   END
declare teta cursor FORWARD_ONLY FOR
                    SELECT DISTINCT * FROM #tempteta
open teta
 fetch next from teta into @docu
 while @@FETCH_STATUS = 0
   BEGIN
      IF exists(SELECT  TOP 1 idagente FROM tb_Agentes WHERE documento=@docu )
                 seLECT 'tb_agentes----' as Tabla, idagente, documento,apellido,nombre FROM tb_Agentes WHERE documento=@docu 
        IF exists(SELECT  TOP 1 nrodoc FROM tb_padron WHERE nrodoc=@docu )
                 seLECT 'tb_padron-----' as Tabla, idpadron, nrodoc,apellido,nombre FROM tb_padron WHERE nrodoc=@docu                  
      fetch next from teta into @Docu
  END
close teta
deallocate teta
drop table [#TEMPteta]
GO
